package edu.neusoft.kongfanfei317.yuejian1.activity;

import android.net.Uri;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.ImageButton;
import android.widget.LinearLayout;

import edu.neusoft.kongfanfei317.yuejian1.R;
import edu.neusoft.kongfanfei317.yuejian1.fragment.CollectFragment;
import edu.neusoft.kongfanfei317.yuejian1.fragment.MeFragment;
import edu.neusoft.kongfanfei317.yuejian1.fragment.SearchFragment;
import edu.neusoft.kongfanfei317.yuejian1.fragment.ClubFragment;

public class MainActivity extends BaseActivity implements View.OnClickListener,ClubFragment.
        OnFragmentInteractionListener,CollectFragment.OnFragmentInteractionListener,
        SearchFragment.OnFragmentInteractionListener,MeFragment.OnFragmentInteractionListener{
    LinearLayout tab_shop,tab_collect,tab_search,tab_me;
    ImageButton img_shop,img_collect,img_search,img_me;
    Fragment fragment_shop,fragment_collect,fragment_search,fragment_me;
    FragmentManager fragmentManager;
    FragmentTransaction fragmentTransaction;


    @Override
    void initView() {
        setLayout_file(R.layout.activity_main);
        tab_shop = (LinearLayout)findViewById(R.id.tab_shop);
        tab_collect = (LinearLayout)findViewById(R.id.tab_collect);
        tab_search = (LinearLayout)findViewById(R.id.tab_search);
        tab_me = (LinearLayout)findViewById(R.id.tab_me);
        img_shop = (ImageButton)findViewById(R.id.img_shop);
        img_collect = (ImageButton)findViewById(R.id.img_collect);
        img_search = (ImageButton)findViewById(R.id.img_search);
        img_me = (ImageButton)findViewById(R.id.img_me);
        setSelect(0);
    }

    @Override
    void initEvent() {
        tab_shop.setOnClickListener(this);
        tab_collect.setOnClickListener(this);
        tab_search.setOnClickListener(this);
        tab_me.setOnClickListener(this);
    }

    @Override
    void initData() {

    }

    @Override
    public void onClick(View view) {
        resetImage();
        switch (view.getId()){
            case R.id.tab_shop:
                setSelect(0);
                break;
            case R.id.tab_collect:
                setSelect(1);
                break;
            case R.id.tab_search:
                setSelect(2);
                break;
            case R.id.tab_me:
                setSelect(3);
                break;
            default:
                break;
        }

    }
    private void resetImage(){
        img_shop.setImageResource(R.drawable.ic_shop_n);
        img_collect.setImageResource(R.drawable.ic_collect_n);
        img_search.setImageResource(R.drawable.ic_search_n);
        img_me.setImageResource(R.drawable.ic_me_n);
    }
    private void setSelect(int i){
        fragmentManager=getSupportFragmentManager();
        fragmentTransaction=fragmentManager.beginTransaction();
        hideFragment(fragmentTransaction);
        switch (i){
            case 0:
                if(fragment_shop==null){
                    fragment_shop = new ClubFragment();
                    fragmentTransaction.add(R.id.content,fragment_shop);
                }
                else {
                    fragmentTransaction.show(fragment_shop);
                }
                img_shop.setImageResource(R.drawable.ic_shop_y);
                break;
            case 1:
                if(fragment_collect==null){
                    fragment_collect = new CollectFragment();
                    fragmentTransaction.add(R.id.content,fragment_collect);
                }
                else {
                    fragmentTransaction.show(fragment_collect);
                }
                img_collect.setImageResource(R.drawable.ic_collect_y);
                break;
            case 2:
                if(fragment_search==null){
                    fragment_search = new SearchFragment();
                    fragmentTransaction.add(R.id.content,fragment_search);
                }
                else {
                    fragmentTransaction.show(fragment_search);
                }
                img_search.setImageResource(R.drawable.ic_search_y);
                break;
            case 3:
                if(fragment_me==null){
                    fragment_me = new MeFragment();
                    fragmentTransaction.add(R.id.content,fragment_me);
                }
                else {
                    fragmentTransaction.show(fragment_me);
                }
                img_me.setImageResource(R.drawable.ic_me_y);
                break;
            default:
                break;
        }
        fragmentTransaction.commit();
    }

    @Override
    public void onFragmentInteraction(Uri uri) {

    }
    private void hideFragment(FragmentTransaction fragmentTransaction){
        if (fragment_shop!=null)
            fragmentTransaction.hide(fragment_shop);
        if (fragment_collect!=null)
            fragmentTransaction.hide(fragment_collect);
        if (fragment_search!=null)
            fragmentTransaction.hide(fragment_search);
        if (fragment_me!=null)
            fragmentTransaction.hide(fragment_me);

    }

}